package cc.vv.btpartyjob.homepage.fragment.rank;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dawning.utiming.task.api.HomeworkAPI;

import java.util.ArrayList;
import java.util.HashMap;

import cc.vv.baselibrary.api.BaseUrl;
import cc.vv.baselibrary.bean.PublicViewHolder;
import cc.vv.baselibrary.fragment.BaseFragment;
import cc.vv.baselibrary.global.UserInfoSharePreKey;
import cc.vv.baselibrary.util.LKAppUtil;
import cc.vv.baselibrary.util.LKPrefUtil;
import cc.vv.baselibrary.util.UserInfoManageUtil;
import cc.vv.baselibrary.view.BCNoScrollListView;
import cc.vv.baselibrary.view.LKCircleImageView;
import cc.vv.btpartyjob.homepage.R;
import cc.vv.btpartyjob.homepage.adapter.BranchRankAdapter;
import cc.vv.btpartyjob.homepage.adapter.LearnRankAllAdapter;
import cc.vv.btpartyjob.homepage.moudel.bean.RankingObj;
import cc.vv.btpartyjob.homepage.moudel.bean.rank.BranchRankingObj;
import cc.vv.lkbasecomponent.http.LKHttp;
import cc.vv.lkbasecomponent.http.lib.CallBack;
import cc.vv.lkimagecomponent2.LKImage;


/**
 * 月度排名
 *
 * @author chaohaipeng  on 2018/6/15.
 */

public class MouthRankFragment extends BaseFragment {

    private int frist, second, third;
    private int fristCount, secondCount, thirdCount;//1、2、3排名数量
    private int fristStatus, secondStatus, thirdStatus;//1、2、3状态
    private String fristListId, secondListId, thirdListId;//排名1、2、3id

    private String fristId, secondId, thirdId;//排名用户1、2、3id
    private ArrayList<BranchRankingObj> mRankList = new ArrayList<>();
    private ArrayList<BranchRankingObj> mBranchRankList = new ArrayList<>();
    private BranchRankAdapter mAdapter;
    private LearnRankAllAdapter mAllAdapter;


    @Override
    protected int initLayoutRes() {
        return R.layout.activity_learn_rank;
    }

    @Override
    protected void initData(@Nullable Bundle bundle) {
        super.initData(bundle);
        getLoading().showLoading();
    }

    @Override
    protected void initView(Bundle bundle) {
        super.initView(bundle);
        holder.rl_rank_frist.setVisibility(View.INVISIBLE);
        holder.rl_rank_second.setVisibility(View.INVISIBLE);
        holder.rl_rank_thrid.setVisibility(View.INVISIBLE);
        requestData();
        mAdapter = new BranchRankAdapter(mBranchRankList, mActivity);
        holder.lv_zb_ranking.setAdapter(mAdapter);
        mAllAdapter = new LearnRankAllAdapter(mRankList, mActivity);
        holder.lv_all_ranking.setAdapter(mAllAdapter);

        holder.lv_zb_ranking.setFocusable(false);
        holder.lv_all_ranking.setFocusable(false);

        try {
            ArrayList<BranchRankingObj> yue_top = LKPrefUtil.getObject(UserInfoSharePreKey.YUE_PAIMING_TOP, new ArrayList<BranchRankingObj>());
            if (null != yue_top) {
                allRank(yue_top);
            }
            ArrayList<BranchRankingObj> yue_bottom = LKPrefUtil.getObject(UserInfoSharePreKey.YUE_PAIMING_BOTTOM, new ArrayList<BranchRankingObj>());
            if (null != yue_bottom) {
                mBranchRankList.addAll(yue_bottom);
                mAdapter.notifyDataSetChanged();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private ViewHolder holder = null;

    @Override
    protected PublicViewHolder initViewHolderObject() {
        holder = new ViewHolder();
        return holder;
    }

    @Override
    protected void baseOnClick(int viewId) {

    }

    private class ViewHolder extends PublicViewHolder {

        private LKCircleImageView iv_frist_head;
        private LKCircleImageView iv_second_head;
        private LKCircleImageView iv_thrid_head;
        private TextView tv_ranking_frist_name;
        private TextView tv_ranking_second_name;
        private TextView tv_ranking_thrid_name;
        private TextView tv_ranking_frist_time;
        private TextView tv_ranking_second_time;
        private TextView tv_ranking_thrid_time;
        private ImageView iv_ranking_second;
        private ImageView iv_frist_rank;
        private ImageView iv_ranking_thrid;
        private TextView ranking_frist_zan;
        private TextView ranking_second_zan;
        private TextView ranking_thrid_zan;
        private BCNoScrollListView lv_all_ranking;
        private BCNoScrollListView lv_zb_ranking;
        private RelativeLayout rl_rank_frist;
        private RelativeLayout rl_rank_second;
        private RelativeLayout rl_rank_thrid;
    }

    /**
     * 在线排名解析
     */
    private void allRank(ArrayList<BranchRankingObj> rankList) {
        if (rankList.size() >= 3) {
            holder.rl_rank_frist.setVisibility(View.VISIBLE);
            holder.rl_rank_second.setVisibility(View.VISIBLE);
            holder.rl_rank_thrid.setVisibility(View.VISIBLE);
        } else if (rankList.size() == 2) {
            holder.rl_rank_frist.setVisibility(View.VISIBLE);
            holder.rl_rank_second.setVisibility(View.VISIBLE);
            holder.rl_rank_thrid.setVisibility(View.INVISIBLE);
        } else if (rankList.size() == 1) {
            holder.rl_rank_frist.setVisibility(View.VISIBLE);
            holder.rl_rank_second.setVisibility(View.INVISIBLE);
            holder.rl_rank_thrid.setVisibility(View.INVISIBLE);
        } else if (rankList.size() == 0) {
            holder.rl_rank_frist.setVisibility(View.INVISIBLE);
            holder.rl_rank_second.setVisibility(View.INVISIBLE);
            holder.rl_rank_thrid.setVisibility(View.INVISIBLE);
        }

        for (int i = 0; i < rankList.size(); i++) {
            if (i == 0) {
                BranchRankingObj user = rankList.get(0);

                if (user != null) {
                    holder.ranking_frist_zan.setText(user.getMonthPraiseCount() + "");
                    holder.tv_ranking_frist_name.setText(user.getName());
                    holder.tv_ranking_frist_time.setText(time(rankList.get(0).getTotalHours() + ""));
                    //头像
                    LKImage.load().placeHolder(R.mipmap.icon_def_image)
                            .error(R.mipmap.icon_def_image).load(BaseUrl.getYanAnUrl() + user.getPhotoPath()).into(holder.iv_frist_head);
                    if (fristStatus == 1) {
                        holder.iv_frist_rank.setImageResource(R.mipmap.icon_emblem_red);
                    } else if (fristStatus == -1) {
                        holder.iv_frist_rank.setImageResource(R.mipmap.icon_emblem_gray);
                    }
                }
            } else if (i == 1) {
                BranchRankingObj user = rankList.get(1);
                if (user != null) {
                    holder.ranking_second_zan.setText(user.getMonthPraiseCount() + "");
                    holder.tv_ranking_second_name.setText(user.getName());
                    holder.tv_ranking_second_time.setText(time(rankList.get(1).getTotalHours() + ""));
                    LKImage.load().placeHolder(R.mipmap.icon_def_image)
                            .error(R.mipmap.icon_def_image).load(BaseUrl.getYanAnUrl() + user.getPhotoPath()).into(holder.iv_second_head);

                    if (secondStatus == 1) {
                        holder.iv_ranking_second.setImageResource(R.mipmap.icon_emblem_red);
                    } else if (secondStatus == -1) {
                        holder.iv_ranking_second.setImageResource(R.mipmap.icon_emblem_gray);
                    }
                }
            } else if (i == 2) {
                BranchRankingObj user = rankList.get(2);
                if (user != null) {
                    holder.ranking_thrid_zan.setText(user.getMonthPraiseCount() + "");
                    holder.tv_ranking_thrid_name.setText(user.getName());
                    holder.tv_ranking_thrid_time.setText(time(rankList.get(2).getTotalHours() + ""));
                    LKImage.load().placeHolder(R.mipmap.icon_def_image)
                            .error(R.mipmap.icon_def_image).load(BaseUrl.getYanAnUrl() + user.getPhotoPath()).into(holder.iv_thrid_head);

                    if (thirdStatus == 1) {
                        holder.iv_ranking_thrid.setImageResource(R.mipmap.icon_emblem_red);
                    } else if (thirdStatus == -1) {
                        holder.iv_ranking_thrid.setImageResource(R.mipmap.icon_emblem_gray);
                    }
                }
            } else {
                mRankList.add(rankList.get(i));
            }
        }
        mAllAdapter.notifyDataSetChanged();
    }

    /**
     * 请求数据
     */
    private void requestData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("SESSIONID", UserInfoManageUtil.getUserInfo().getSESSIONID());
        LKHttp.post(HomeworkAPI.INSTANCE.getStudyRankMonthList()
                , params, RankingObj.class, new CallBack<RankingObj>() {
                    @Override
                    public void onSuccess(String url, RankingObj obj) {
                        super.onSuccess(url, obj);
                        if (obj != null) {

                            ArrayList<BranchRankingObj> rankList = obj.getRankList();
                            if (rankList != null && rankList.size() > 0) {
                                allRank(rankList);
                                LKPrefUtil.putObject(UserInfoSharePreKey.YUE_PAIMING_TOP, rankList);
                            }
                            ArrayList<BranchRankingObj> branchRankList = obj.getBranchRankList();
                            if (branchRankList != null && branchRankList.size() > 0) {
                                mBranchRankList.clear();
                                LKPrefUtil.putObject(UserInfoSharePreKey.YUE_PAIMING_BOTTOM, branchRankList);
                                mBranchRankList.addAll(branchRankList);
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onFinish(String url, int resultCode, boolean isLoading) {
                        super.onFinish(url, resultCode, isLoading);
                        getLoading().closeLoading();
                    }


                    @Override
                    public void onSuccess(String url, String resultText, RankingObj obj) {
                        super.onSuccess(url, resultText, obj);
                    }
                }, true);
    }


    /**
     * 时间转换
     *
     * @param time
     * @return
     */
    private String time(String time) {
        if (!LKAppUtil.getInstance().isNumeric(time)) {
            return 0 + "分钟";
        }
        Long total = Long.parseLong(time);
        if (total / 3600 == 0 && total / 60 == 0) {
            return 0 + "分钟";
        } else if (total / 3600 == 0 && total / 60 > 0) {
            return total / 60 + "分钟";
        } else if ((total / 3600) > 0 && (total / (3600 * 24)) == 0) {
            return total / 3600 + "小时" + ((total - ((total / 3600) * 3600)) / 60) + "分钟";

        } else if ((total / (3600 * 24)) > 0 && (total / (3600 * 24 * 30)) == 0) {
            return (total / (3600 * 24)) + "天" + ((total - ((total / (3600 * 24)) * (3600 * 24))) / 3600) + "小时";
        } else if ((total / (3600 * 24 * 30)) > 0) {
            return (total / (3600 * 24 * 30)) + "月" + ((total - ((total / (3600 * 24 * 30)) * (3600 * 24 * 30))) / (3600 * 24)) + "天";
        }
        return 0 + "分钟";
    }


}
